const db = wx.cloud.database().collection("notice").doc("index_notice")
const users = wx.cloud.database().collection("users")
const app=getApp()
var util=require('../../utils/util'); 
Page({
  data: {
    
  },
  dazhuanpan:function(){
    users.where({
      _openid: app.globalData.openid
    }).get({
      success(res) {
        var timeof = util.formatTime(new Date)
        if (res.data[0].sharetime.indexOf(timeof) === -1) {
          wx.showModal({
            title: '提示',
            content: '请分享后再进行大转盘哦!',
            showCancel:false,
            confirmText:'这就去',
          })
        }
        else
        {
          wx.navigateTo({
            url: '/pages/dazhuanpan/dazhuanpan',
          })
        }
      }
    })
  },
  hongbao:function(){
    wx.navigateTo({
      url: '/pages/hongbao/hongbao',
    })
  },
  onLoad: function (e) {
    var that=this
    db.get({//公告
      success(res){
        var notices=res.data.notice
        var msgList=[]
        for(var i=0;i<=notices.length-1;++i){
          msgList.push({ url: "url", title: notices[i] },);
        }
      that.setData({
        msgList:msgList
      })
      wx.showModal({
        title: '提示',
        content: '礼品结算请联系我们微信：v18810078825',
        showCancel:false,
        confirmText:'确定',
      })
    }
    })
    
  },
  onShareAppMessage: function () {
    var that = this
    var useropenid=''
    wx.cloud.callFunction({
      name:"getuseropenid",
      success(res){
        useropenid=res.result.openid
      }
    })
    users.where({
      _openid: useropenid
    }).get({
      success(res) {
        var timeof = util.formatTime(new Date)
        if (res.data[0].sharetime.indexOf(timeof) === -1) {
          var frequency = res.data[0].frequency
          var sharetime = res.data[0].sharetime
          var _id = res.data[0]._id
          sharetime.push(timeof)
          frequency++
          users.doc(_id).update({
            data: {
              frequency: frequency,
              sharetime: sharetime
            },
          })
          app.globalData.frequency = frequency
        }
      }
    })
  },
  onShareTimeline: function () {
    var that = this
    var useropenid=''
    wx.cloud.callFunction({
      name:"getuseropenid",
      success(res){
        useropenid=res.result.openid
      }
    })
    users.where({
      _openid: useropenid
    }).get({
      success(res) {
        var timeof = util.formatTime(new Date)
        if (res.data[0].sharetime.indexOf(timeof) === -1) {
          var frequency = res.data[0].frequency
          var sharetime = res.data[0].sharetime
          var _id = res.data[0]._id
          sharetime.push(timeof)
          frequency++
          users.doc(_id).update({
            data: {
              frequency: frequency,
              sharetime: sharetime
            },
          })
          app.globalData.frequency = frequency
        }
      }
    })
  },
  
})